class_name Monster extends Node

# 基本信息
var id: String
var monster_name: String
var description: String
var monster_type: String  # 普通、稀有、史诗、传说
var element: String  # 属性类型
var sprite_path: String  # 精灵图路径

# 等级和经验
var level: int = 1
var experience: int = 0
var experience_to_next_level: int = 100
var star_level: int = 1  # 星级，影响成长上限

# 基础属性
var strength: int  # 力量，影响物理攻击
var agility: int  # 敏捷，影响速度和闪避
var intelligence: int  # 智力，影响魔法攻击和魔法防御
var endurance: int  # 耐力，影响生命值和物理防御
var spirit: int  # 灵力，影响技能效果和抗性

# 战斗属性
var max_health: int
var current_health: int
var attack: int
var defense: int
var magic_attack: int
var magic_defense: int
var speed: int
var evasion: float
var accuracy: float

# 成长系数
var strength_growth: float
var agility_growth: float
var intelligence_growth: float
var endurance_growth: float
var spirit_growth: float

# 技能和状态
var skills = []
var current_status = []

# 初始化函数
func _init(monster_data: Dictionary):
    id = monster_data.get("id", "")
    monster_name = monster_data.get("name", "未命名幻兽")
    description = monster_data.get("description", "")
    monster_type = monster_data.get("type", "普通")
    element = monster_data.get("element", "无")
    sprite_path = monster_data.get("sprite_path", "")
    
    # 设置基础属性
    strength = monster_data.get("base_strength", 5)
    agility = monster_data.get("base_agility", 5)
    intelligence = monster_data.get("base_intelligence", 5)
    endurance = monster_data.get("base_endurance", 5)
    spirit = monster_data.get("base_spirit", 5)
    
    # 设置成长系数
    strength_growth = monster_data.get("strength_growth", 1.0)
    agility_growth = monster_data.get("agility_growth", 1.0)
    intelligence_growth = monster_data.get("intelligence_growth", 1.0)
    endurance_growth = monster_data.get("endurance_growth", 1.0)
    spirit_growth = monster_data.get("spirit_growth", 1.0)
    
    # 初始化技能
    var initial_skills = monster_data.get("initial_skills", [])
    for skill_id in initial_skills:
        # 这里将来会从技能数据库加载技能
        skills.append(skill_id)
    
    # 计算战斗属性
    _calculate_battle_stats()

# 计算战斗属性
func _calculate_battle_stats():
    max_health = 20 + (endurance * 5) + (level * 5)
    current_health = max_health
    attack = strength * 2 + level
    defense = endurance + (strength / 2)
    magic_attack = intelligence * 2 + level
    magic_defense = spirit + (intelligence / 2)
    speed = agility * 2 + (level / 2)
    evasion = agility * 0.5
    accuracy = 95 + (agility * 0.2)

# 升级函数
func level_up():
    level += 1
    
    # 根据成长系数增加属性
    strength += int(strength_growth * (1 + (star_level * 0.1)))
    agility += int(agility_growth * (1 + (star_level * 0.1)))
    intelligence += int(intelligence_growth * (1 + (star_level * 0.1)))
    endurance += int(endurance_growth * (1 + (star_level * 0.1)))
    spirit += int(spirit_growth * (1 + (star_level * 0.1)))
    
    # 重新计算战斗属性
    _calculate_battle_stats()
    
    # 设置下一级所需经验
    experience_to_next_level = int(experience_to_next_level * 1.5)
    
    # 检查是否可以学习新技能
    _check_new_skills()
    
    return {
        "strength_gain": int(strength_growth * (1 + (star_level * 0.1))),
        "agility_gain": int(agility_growth * (1 + (star_level * 0.1))),
        "intelligence_gain": int(intelligence_growth * (1 + (star_level * 0.1))),
        "endurance_gain": int(endurance_growth * (1 + (star_level * 0.1))),
        "spirit_gain": int(spirit_growth * (1 + (star_level * 0.1)))
    }

# 检查是否可以学习新技能
func _check_new_skills():
    # 这里将来会从技能数据库检查并添加新技能
    pass

# 增加经验值
func add_experience(amount: int):
    experience += amount
    
    # 检查是否可以升级
    while experience >= experience_to_next_level:
        experience -= experience_to_next_level
        var gains = level_up()
        print("%s 升级到 %d 级!" % [monster_name, level])
        print("属性提升: 力量+%d, 敏捷+%d, 智力+%d, 耐力+%d, 灵力+%d" % 
            [gains.strength_gain, gains.agility_gain, gains.intelligence_gain, 
             gains.endurance_gain, gains.spirit_gain])

# 星级提升
func increase_star_level():
    if star_level < 5:  # 最高5星
        star_level += 1
        print("%s 的星级提升到 %d 星!" % [monster_name, star_level])
        # 星级提升后重新计算属性
        _calculate_battle_stats()
        return true
    return false

# 使用技能
func use_skill(skill_index: int, target):
    if skill_index < 0 or skill_index >= skills.size():
        print("无效的技能索引")
        return null
    
    var skill_id = skills[skill_index]
    # 这里将来会从技能数据库获取技能效果并应用
    print("%s 使用了技能: %s" % [monster_name, skill_id])
    
    # 模拟技能效果
    var damage = 0
    if skill_id.begins_with("physical"):
        damage = attack - target.defense
    elif skill_id.begins_with("magical"):
        damage = magic_attack - target.magic_defense
    
    damage = max(1, damage)  # 确保至少造成1点伤害
    
    return {
        "damage": damage,
        "effects": []  # 这里将来会添加技能附加效果
    }

# 受到伤害
func take_damage(amount: int):
    current_health -= amount
    current_health = max(0, current_health)
    print("%s 受到 %d 点伤害，剩余生命值: %d/%d" % [monster_name, amount, current_health, max_health])
    return current_health <= 0  # 返回是否已经被击败

# 恢复生命值
func heal(amount: int):
    current_health += amount
    current_health = min(current_health, max_health)
    print("%s 恢复了 %d 点生命值，当前生命值: %d/%d" % [monster_name, amount, current_health, max_health])

# 获取幻兽状态信息
func get_status():
    return {
        "id": id,
        "name": monster_name,
        "type": monster_type,
        "element": element,
        "level": level,
        "star_level": star_level,
        "health": current_health,
        "max_health": max_health,
        "experience": experience,
        "experience_to_next_level": experience_to_next_level,
        "strength": strength,
        "agility": agility,
        "intelligence": intelligence,
        "endurance": endurance,
        "spirit": spirit,
        "skills": skills
    }
